layui.define(['table', 'utils', 'ajax', 'laypage', 'form'], function(exports){
	var $ = layui.jquery;
	var utils = layui.utils;
	var table = layui.table;
	var ajax = layui.ajax;
	var conf = layui.conf;
	var laypage = layui.laypage;
	
	
	
	/**
	 * 分页表格函数
	 * elem 待渲染的表格元素
	 * url 请求连接
	 * */
	TablePageManager = function(elem, url, o = {}){
		this.elem = elem;
		this.lay_table_index = null;
		this.url = url;
		this.pageDataName = conf.response.pageDataName || "data.list";
		this.pageCountName = conf.response.pageCountName || "data.totalRow";
		this.pageNumberName = conf.response.pageNumberName || "data.pageNumber"
		this.pageSizeName = conf.response.pageSizeName || "data.pageSize"
		this.pageNumber = 1;
		this.pageSize = 20;
		this.pageOptions = {
			limits : [10, 20, 30, 40, 50],
			groups : 3, // 连续显示分页数
			prev : '<em><</em>', // 若不显示，设置false即可
			next : '<em>></em>', // 若不显示，设置false即可
			first : '1', // 将首页显示为数字1,。若不显示，设置false即可
			last : '', // 将尾页显示为总页数。若不显示，设置false即可
			layout : ['prev', 'page', 'next', 'limit', 'count', 'skip'],
			theme : '',
			that : this,
			jump : this.pageJump
		},
		this.params = {};
		this.data = [];
		this.tableOptions = $.extend({
			elem : this.elem,
//			height: 'full-200',
			page : true,
			limit : this.pageSize,
			skin : "row",
			even : true,
			id:new Date().getTime() + ""
		}, o);
		this.searchFilter = o.searchFilter;
		this.listenerSearchFilter();
	}
	
	TablePageManager.prototype.setUrl = function(url){this.url = url}
	TablePageManager.prototype.setPageNumber = function(pageNumber){this.pageNumber = this.pageOptions.curr = pageNumber;}
	TablePageManager.prototype.setPageSize = function(pageSize){this.pageSize = this.pageOptions.limit = this.tableOptions.limit = pageSize}
	TablePageManager.prototype.setCols = function(cols){tableOptions.cols =cols}
	TablePageManager.prototype.setParam = function(key, value){
		var that = this;
		if(typeof(key) === 'object'){
			layui.each(key, function(k, v){
				that.addParam(k, v);
			})
		}else{
			that.addParam(key, value);
		}
	}
	TablePageManager.prototype.addParam = function(key, value){
		if(utils.isBlank(value)){
			delete this.params[key]
		}else{
			this.params[key] = value
		}
	}
	
	TablePageManager.prototype.listenerSearchFilter = function(){
		if(this.searchFilter){
			var that = this;
			var form = layui.form;
			form.on('submit(' + this.searchFilter +')', function(data){
				that.setParam(data.field);
				that.setPageNumber(1);
				that.load();
				return false;
			});
		}
		
		
		
//		searchForm && $(that.searchForm).on("submit", function(e){
//			var d = {}
//			var keyValues = $(this).serializeArray();
//			for(var i in keyValues){
//				d[keyValues[i]["name"]] = keyValues[i]["value"]; 
//			}
//			that.setParam(d);
//			that.setPageNumber(1);
//			that.load();
//			layui.stope(e)
//			return false;
//		});
	}
	
	
	TablePageManager.prototype.load = function(){
		//因为layui当前版本不支持分页自定义返回数据格式，因此自己获取数据，通过options.data属性渲染表格
		that = this;
		var where = this.params;
		if(!that.tableOptions.page){
			delete where[conf.request.pageNumberName]
			delete where[conf.request.pageSizeName]
		}else{
			where[conf.request.pageNumberName] = that.pageNumber;
			where[conf.request.pageSizeName] = that.pageSize;
		}
		
		ajax.get(this.url, where, {
			showTip : {
				ok : false				
			},
			ok : function(resp){
				that.tableOptions.data = that.data = utils.getDictData(resp, that.pageDataName) || [];
				//如果不分页，则显示所有获取到的数据
				that.tableOptions.page || that.setPageSize(that.data.length);
				var oo = that.lay_table_index ? table.reload(that.tableOptions.id, that.tableOptions) : table.render(that.tableOptions);
				that.lay_table_index = oo.config.index;
				//重新渲染分页，如果不渲染，分页条数只会根据数据条数设置。
				if(that.tableOptions.page){
					var totalRow = utils.getDictData(resp, that.pageCountName);
					that.setPageNumber(utils.getDictData(resp, that.pageNumberName) || that.pageNumber);
					that.setPageSize(utils.getDictData(resp, that.pageSizeName) || that.pageSize);
					totalRow && totalRow >0 && laypage.render($.extend({}, that.pageOptions, {
						elem : 'layui-table-page' + that.lay_table_index,
						count : totalRow,
					}));
				}
			}
		});
	}
	
	TablePageManager.prototype.pageJump = function(obj, first){
		//首次不执行
	    if(!first){
	    	this.that.setPageNumber(obj.curr);
	    	this.that.setPageSize(obj.limit);
	    	this.that.load();
	    }
	}
	
	exports("tablePage", {
		create : function(dom, url, o){
			return new TablePageManager(dom, url, o);
		}
	})
	
	
})